Skip to content

Conversation

@milos1397
Copy link
Contributor

@milos1397 milos1397 commented Dec 15, 2025

Fixes #171883.

Basically, if the operand of the phi is an Instruction but it's not available, the condition would just break, and when we reach the deferral check, execution would continue even though there is a non-Instruction operand, leading to a crash in the subsequent processing loop.

@milos1397 milos1397 requested a review from nikic as a code owner December 15, 2025 16:31
@github-actions
Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot llvmbot added llvm:instcombine Covers the InstCombine, InstSimplify and AggressiveInstCombine passes llvm:transforms labels Dec 15, 2025
@llvmbot
Copy link
Member

llvmbot commented Dec 15, 2025

@llvm/pr-subscribers-llvm-transforms

Author: Miloš Poletanović (milos1397)

Changes

Handles issue #171883

Basically, if the operand of the $\phi$ is an Instruction but it's not available, the condition would just break, and when we reach the deferral check, execution would continue even though there is a non-Instruction operand, leading to a crash in the subsequent processing loop.


Full diff: https://github.com/llvm/llvm-project/pull/172332.diff

1 Files Affected:

  • (modified) llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp (+6-6)
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp b/llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp
index 9491610190c10..ea5587f4c9412 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp
@@ -296,12 +296,12 @@ bool PointerReplacer::collectUsers() {
       /// TODO: Handle poison and null pointers for PHI and select.
       // If all incoming values are available, mark this PHI as
       // replacable and push it's users into the worklist.
-      bool IsReplaceable = true;
-      if (all_of(PHI->incoming_values(), [&](Value *V) {
-            if (!isa<Instruction>(V))
-              return IsReplaceable = false;
-            return isAvailable(cast<Instruction>(V));
-          })) {
+      bool IsReplaceable = all_of(PHI->incoming_values(), [](Value *V) {
+        return isa<Instruction>(V);
+      });
+      if (IsReplaceable && all_of(PHI->incoming_values(), [&](Value *V) {
+        return isAvailable(cast<Instruction>(V));
+      })) {
         UsersToReplace.insert(PHI);
         PushUsersToWorklist(PHI);
         continue;

@gandhi56 gandhi56 self-requested a review December 15, 2025 17:55
@gandhi56
Copy link
Contributor

@milos1397 Could you please reduce and attach the test case? The change seems fine to me.

%l_631.0 = phi ptr addrspace(5) [ %l_631.1, %BS_LABEL_7 ], [ %arrayidx, %sw.epilog ], [ undef, %entry ], [ undef, %entry ]
%cmp = icmp ugt ptr addrspace(5) %l_631.0, null
br label %BS_LABEL_7
} No newline at end of file
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing newline

@@ -0,0 +1,29 @@
; RUN: opt < %s -O1 -S | FileCheck %s
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test should only run -passes=instcombine. Please also use update_test_checks.py.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @nikic , thanks for pointing out, I've updated the PR.

@nikic nikic enabled auto-merge (squash) December 17, 2025 10:00
@github-actions
Copy link

github-actions bot commented Dec 17, 2025

✅ With the latest revision this PR passed the C/C++ code formatter.

@github-actions
Copy link

github-actions bot commented Dec 17, 2025

✅ With the latest revision this PR passed the undef deprecator.

@nikic nikic disabled auto-merge December 17, 2025 10:02
@milos1397
Copy link
Contributor Author

milos1397 commented Dec 17, 2025

Using poison instead of undef (deprecated). @nikic

@nikic nikic enabled auto-merge (squash) December 17, 2025 11:45
@nikic nikic merged commit 44a52ea into llvm:main Dec 17, 2025
9 of 10 checks passed
@github-actions
Copy link

@milos1397 Congratulations on having your first Pull Request (PR) merged into the LLVM Project!

Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR.

Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues.

How to do this, and the rest of the post-merge process, is covered in detail here.

If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again.

If you don't get any reports, no action is required from you. Your changes are working as expected, well done!

@llvm-ci
Copy link
Collaborator

llvm-ci commented Dec 17, 2025

LLVM Buildbot has detected a new failure on builder ml-opt-dev-x86-64 running on ml-opt-dev-x86-64-b2 while building llvm at step 6 "test-build-unified-tree-check-all".

Full details are available at: https://lab.llvm.org/buildbot/#/builders/137/builds/30766

Here is the relevant piece of the build log for the reference
Step 6 (test-build-unified-tree-check-all) failure: test (failure)
******************** TEST 'LLVM :: CodeGen/MLRegAlloc/dev-mode-log-2-fcts.ll' FAILED ********************
Exit Code: 1

Command Output (stdout):
--
# RUN: at line 6
/b/ml-opt-dev-x86-64-b1/build/bin/llc -mtriple=x86_64-linux-unknown -regalloc=greedy -regalloc-enable-advisor=development    -regalloc-training-log=/b/ml-opt-dev-x86-64-b1/build/test/CodeGen/MLRegAlloc/Output/dev-mode-log-2-fcts.ll.tmp1 < /b/ml-opt-dev-x86-64-b1/llvm-project/llvm/test/CodeGen/MLRegAlloc/dev-mode-log-2-fcts.ll
# executed command: /b/ml-opt-dev-x86-64-b1/build/bin/llc -mtriple=x86_64-linux-unknown -regalloc=greedy -regalloc-enable-advisor=development -regalloc-training-log=/b/ml-opt-dev-x86-64-b1/build/test/CodeGen/MLRegAlloc/Output/dev-mode-log-2-fcts.ll.tmp1
# .---command stdout------------
# | 	.file	"<stdin>"
# | 	.section	.text.unlikely.,"ax",@progbits
# | 	.globl	f1                              # -- Begin function f1
# | 	.p2align	4
# | 	.type	f1,@function
# | f1:                                     # @f1
# | 	.cfi_startproc
# | # %bb.0:
# | 	pushq	%r14
# | 	.cfi_def_cfa_offset 16
# | 	pushq	%rbx
# | 	.cfi_def_cfa_offset 24
# | 	pushq	%rax
# | 	.cfi_def_cfa_offset 32
# | 	.cfi_offset %rbx, -24
# | 	.cfi_offset %r14, -16
# | 	movq	%rdx, %rbx
# | 	movq	%rdi, %r14
# | 	addq	%rsi, %r14
# | 	callq	f@PLT
# | 	movq	%r14, (%rbx)
# | 	addq	$8, %rsp
# | 	.cfi_def_cfa_offset 24
# | 	popq	%rbx
# | 	.cfi_def_cfa_offset 16
# | 	popq	%r14
# | 	.cfi_def_cfa_offset 8
# | 	retq
# | .Lfunc_end0:
# | 	.size	f1, .Lfunc_end0-f1
# | 	.cfi_endproc
# |                                         # -- End function
# | 	.section	.text.hot.,"ax",@progbits
# | 	.globl	f2                              # -- Begin function f2
# | 	.p2align	4
# | 	.type	f2,@function
# | f2:                                     # @f2
# | 	.cfi_startproc
# | # %bb.0:
# | 	addq	%rsi, %rdi
# | 	movq	%rdi, (%rdx)
...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

llvm:instcombine Covers the InstCombine, InstSimplify and AggressiveInstCombine passes llvm:transforms

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[InstCombine] Assertion `isa<To>(Val) && "cast<Ty>() argument of incompatible type!"' failed.

5 participants